<template>
  <div class="divBox">
    <el-card class="box-card">
      <el-form inline size="small" @submit.native.prevent>
        <el-form-item class="el-form-item-align-right">
          <el-button icon="el-icon-circle-plus" size="mini" type="primary" @click="openClassificationEdit()"
                     v-has-permi="['admin:merchantActivities:buying:save']">添加团购分类
          </el-button>
        </el-form-item>
      </el-form>
      <el-table :data="listData.list"
                :header-cell-style=" {fontWeight:'bold',background:'#f8f8f9',color: '#515a6e',height:'40px'}"
                size="mini">
        <el-table-column coverImg="120" prop="id" type="expand">
          <template slot-scope="scope">
            <el-table :data="scope.row.child"
                      :header-cell-style=" {fontWeight:'bold',background:'#f8f8f9',color: '#515a6e',height:'40px'}"
                      size="mini">
              <el-table-column align="center" label="ID" prop="id"/>
              <el-table-column align="center" label="名称" min-width="130" prop="name"/>
              <el-table-column align="center" label="价格" min-width="130" prop="price"/>
              <el-table-column align="center" label="份数" min-width="130" prop="number"/>
              <el-table-column align="center" label="单位" min-width="130" prop="unit"/>
              <el-table-column align="center" label="排序" min-width="80" prop="sort"/>
              <el-table-column align="center" fixed="right" label="操作" min-width="180">
                <template slot-scope="scope">
                  <el-button v-has-permi="['admin:merchantActivities:buying:saveSon']" icon="el-icon-edit" size="mini"
                             type="text"
                             @click="openClassificationSonEdit(null,scope.row)">编辑
                  </el-button>
                  <el-button v-has-permi="['admin:merchantActivities:buying:deleteSon']" icon="el-icon-delete"
                             size="mini"
                             type="text"
                             @click="handlerDeleteSon(scope.row.id)">删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </template>
        </el-table-column>
        <el-table-column coverImg="120" label="ID" prop="id"/>
        <el-table-column label="分类名称" min-width="130" prop="categoryName"/>
        <el-table-column label="排序" min-width="80" prop="sort"/>
        <el-table-column align="center" fixed="right" label="操作" min-width="180">
          <template slot-scope="scope">
            <el-button v-has-permi="['admin:merchantActivities:buying:saveSon']" icon="el-icon-edit" size="mini"
                       type="text"
                       @click="openClassificationSonEdit(scope.row,null)">添加子类
            </el-button>
            <el-button v-has-permi="['admin:merchantActivities:save']" icon="el-icon-edit" size="mini" type="text"
                       @click="openClassificationEdit(scope.row)">编辑
            </el-button>
            <el-button v-has-permi="['admin:merchantActivities:delete']" icon="el-icon-delete" size="mini" type="text"
                       @click="handlerDelete(scope.row.id)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          :current-page="listPram.page"
          :page-sizes="[20,40,60,80,100]"
          :total="listData.count"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </el-card>
    <el-dialog
        :close-on-click-modal="false"
        :title="editClassificationDialog.id? '编辑':'新增'"
        :visible.sync="editClassificationDialog.show"
        destroy-on-close
        top="80px"
        width="800px"
        :modal="false">
      <classification
          :businessId="businessId"
          v-if="editClassificationDialog.show"
          :id="editClassificationDialog.id"
          :tableData="editClassificationDialog.tableData"
          ref="editForm"
          @hideClassificationDialog="hideClassificationDialog"
      />
    </el-dialog>
    <el-dialog
        :close-on-click-modal="false"
        :title="editClassificationSonDialog.id? '编辑':'新增'"
        :visible.sync="editClassificationSonDialog.show"
        destroy-on-close
        top="80px"
        width="800px"
        :modal="false">
      <classification-son
          v-if="editClassificationSonDialog.show"
          :id="editClassificationSonDialog.id"
          :typeId="editClassificationSonDialog.typeId"
          :tableData="editClassificationSonDialog.tableData"
          ref="editForm"
          @hideClassificationSonDialog="hideClassificationSonDialog"
      />
    </el-dialog>
  </div>
</template>

<script>
import {deleteClassificationRow, deleteClassificationSonRow, getClassificationList} from "@/api/merchantActivity";
import {checkPermi} from "@/utils/permission";
import Classification from "@/views/operate/business/merchantActivities/classification.vue";
import ClassificationSon from "@/views/operate/business/merchantActivities/classificationSon.vue";

export default {
  components: {ClassificationSon, Classification},
  props: {
    businessId: {
      default: null
    }
  },
  data() {
    return {
      listData: {list: [], count: 0},
      listPram: {
        page: 1,
        limit: 20,
        businessId: ''
      },
      editDialog: {
        show: false,
        id: null,
      },
      editClassificationDialog: {
        show: false,
        id: null,
        tableData: {}
      },
      editClassificationSonDialog: {
        show: false,
        id: null,
        typeId: null,
        tableData: {}
      },
      editDialogList: {
        show: false,
        id: null,
        editDate: {},
        pid: null
      },
      tableLoading: false,
      imageUrl: ''
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    checkPermi,
    getList() {
      this.tableLoading = true
      this.listPram.businessId = this.businessId
      getClassificationList(this.listPram).then(res => {
        this.listData = res;
      }).finally(() => {
      })
      this.tableLoading = false;
    },
    openClassificationEdit(tableData) {
      if (tableData) {
        this.editClassificationDialog.id = tableData.id;
      }
      this.editClassificationDialog.tableData = tableData;
      this.editClassificationDialog.show = true
    },
    openClassificationSonEdit(typeData, tableData) {
      if (tableData) {
        this.editClassificationSonDialog.id = tableData.id;
      }
      if (typeData) {
        this.editClassificationSonDialog.typeId = typeData.id;
      }
      this.editClassificationSonDialog.tableData = tableData;
      this.editClassificationSonDialog.show = true
    },
    handlerDelete(id) {
      this.$confirm('确认删除该数据吗？').then(() => {
        const loading = this.$loading({text: '删除中', background: 'rgba(255,255,255,.5)'})
        deleteClassificationRow(id, 1).then(() => {
          this.$message.success('删除成功')
          this.getList()
        }).finally(() => {
          loading.close()
        })
      })
    },
    handlerDeleteSon(id) {
      this.$confirm('确认删除该数据吗？').then(() => {
        const loading = this.$loading({text: '删除中', background: 'rgba(255,255,255,.5)'})
        deleteClassificationSonRow(id).then(() => {
          this.$message.success('删除成功')
          this.getList()
        }).finally(() => {
          loading.close()
        })
      })
    },
    hideEditDialog() {
      this.editDialog.show = false
      this.getList()
    },
    hideClassificationDialog() {
      this.editClassificationDialog.show = false
      this.getList()
    },
    hideClassificationSonDialog() {
      this.editClassificationSonDialog.show = false
      this.getList()
    },
    editListDialog() {
      this.editDialogList.show = false
      this.getList()
    },
    handleSizeChange(val) {
      this.listPram.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listPram.page = val
      this.getList()
    },
    resetQuery() {
      this.listPram.isReceive = '';
      this.getList()
    }
  }
}
</script>

<style scoped lang="scss">

</style>

<style scoped>

</style>
